package struts.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import business.PandAction;

import service.vo.MessageVo;
import strtus.form.ActionForm;
import strtus.form.FullBean;
import strtus.form.XmlBean;

public class ActionServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -7127150859111716940L;

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String path = this.getPath(request.getServletPath());
		Map<String,XmlBean> map=(Map<String, XmlBean>) this.getServletContext().getAttribute("struts");
		XmlBean xml=map.get(path);
		String formclass=xml.getFormClass();
		ActionForm form = FullBean.full(formclass,request);
		String actionType=xml.getActionClass();
		Action action=null;
		String url="";
		try {
			Class clazz=Class.forName(actionType);
			action=(Action) clazz.newInstance();
			url=action.execute(request, form, xml.getActionForward());
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("严重：控制器异常");
		}
		RequestDispatcher dis=request.getRequestDispatcher(url);
		dis.forward(request, response);
		//String name = request.getParameter("text");
		//System.out.println(request.getServletPath());
		//String url="";
		//String mess=request.getParameter("");
		/*	boolean b=this.isuser(name);
		MessageVo vo = new MessageVo();
		
		if(b==true){
			
			vo.setDddd("dddd");
			vo.setMess("成功！");
			vo.setName("name");
			vo.setSsss("ssss");
			//request.setAttribute("mess", "成功！");
			//request.setAttribute("x", name);
			//获得了转发器对象
			url ="/view/Success.jsp";
			//RequestDispatcher dis= request.getRequestDispatcher("/view/Success.jsp");
			//dis.forward(request, response);
		}else{
			//request.setAttribute("mess", "失败！");
			//request.setAttribute("x", name);
			vo.setDddd("ddddxx");
			vo.setMess("失败xx！");
			vo.setName("namexx");
			vo.setSsss("ssssxx");
			//获得了转发器对象
			url ="/view/Fail.jsp";
			//RequestDispatcher dis= request.getRequestDispatcher("/view/Fail.jsp");
			//dis.forward(request, response);
		}
		request.setAttribute("mess", vo);*/
		
		//Action action = new PandAction();
		//url=action.execute(request);
		
		//RequestDispatcher dis= request.getRequestDispatcher(url);
		//dis.forward(request, response);
		/*request.setAttribute("x", b);
		//request.setAttribute("x", name);
		//获得了转发器对象
		RequestDispatcher dis= request.getRequestDispatcher("/view/MyJsp.jsp");
		dis.forward(request, response);*/
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}
	
	private String getPath(String servletPath){
		
		return servletPath.split("\\.")[0];
		
	}
	
}
